package com.bidevalution.controller.api;

import com.bidevalution.business.service.BeExpertCommitmentService;
import com.qtp.common.core.TSUser;
import com.qtp.common.data.RedisAccess;
import com.qtp.core.model.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by glodon on 2019-08-13.
 */
@Log4j2
@RestController
@RequestMapping("/beExpertCommitment")
@Api(description = "专家承诺")
public class BeExpertCommitmentController extends BaseController{

    @Value("${download.file.path}")
    private String downloadFileUrl;
    @Autowired
    private BeExpertCommitmentService beExpertCommitmentService;

    @RequestMapping("/queryCommitmentPdf")
    @ApiOperation(value = "专家承诺-查询专家承诺pdf文档", response = String.class, httpMethod = "GET")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段主键", required = true),
            })
    public Response<String> queryCommitmentPdf(HttpServletRequest request, @RequestParam("sectionId") String sectionId) {
        try {
            String userGuid = request.getHeader("userGuid");
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            if(tsUser != null) {
                String fileId = beExpertCommitmentService.convertCommitmentPdf(sectionId, tsUser.getLoginName());
                return Response.ok(downloadFileUrl + "/UploadedFile/" + fileId);
            } else {
                return Response.fail("获取专家账号登录信息失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("查询专家承诺pdf文档失败");
        }
    }

}
